
package com.zsrt.jmj.student.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.zsrt.jmj.student.dao.StudentDao;
import com.zsrt.jmj.student.dao.impl.StudentDaoImpl;
import com.zsrt.jmj.student.entity.StudentEntity;
import com.zsrt.jmj.student.service.StudentService;

public class StudentServiceImpl implements StudentService {

	private StudentDao dao=new StudentDaoImpl();
	@Override
	public void add(StudentEntity entity, Map<String, Object> map) {
		//未完善 验证
		//验证年龄和姓名
		int d = entity.getAge();
		String sa = String.valueOf(d);
		String regEx = "(^\\d{1,3}?$)";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(sa);
		boolean rs = matcher.find();
		if(rs && d<120 && d>0){
			//验证姓名不能为空
			if(null !=entity.getStudentName()){

				try {
					dao.add(entity);
					map.put("result", "success");
					map.put("jieguo", "添加成功...");
					return;
				} catch (Exception e) {
					map.put("result", "failure");
					map.put("jieguo", "添加失败...");
					e.printStackTrace();
					return;
				}
			}else{
				map.put("result", "failure");
				map.put("jieguo", "添加失败，学生姓名不能为空...");
				return;
			}
		}else{
			map.put("result", "failure");
			map.put("jieguo", "添加失败,年龄输入不正确...");
			return;
		}
		
	}
	@Override
	public void findAllStudents(Map<String, Object> map) {
		
		try {
			List<StudentEntity> list = dao.findAllStudents();
			map.put("result", "success");
			map.put("jieguo", "查询成功...");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "failure");
			map.put("jieguo", "查询失败...");
			e.printStackTrace();
		}
		
	}
	@Override
	public void findStudentById(String id, Map<String, Object> map) {
		int sId = Integer.parseInt(id);
		try {
			StudentEntity entity = dao.findStudentById(sId);
			map.put("result", "success");
			map.put("jieguo", "查询成功...");
			map.put("data", entity);
		} catch (SQLException e) {
			map.put("result", "failure");
			map.put("jieguo", "查询失败...");
			e.printStackTrace();
		}
		
	}
	@Override
	public void updateById(StudentEntity entity, Map<String, Object> map) {
		//验证年龄和姓名
		int d = entity.getAge();
		String sa = String.valueOf(d);
		String regEx = "(^\\d{1,3}?$)";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(sa);
		boolean rs = matcher.find();
		if(rs && d<120 && d>0){
			//验证姓名不能为空
			if(null !=entity.getStudentName()){
				try {
					dao.updateById(entity);
					map.put("result", "success");
					map.put("jieguo", "修改成功...");
					return;
				} catch (SQLException e) {
					map.put("result", "failure");
					map.put("jieguo", "修改失败...");
					e.printStackTrace();
					return;
				}
			}else {
				map.put("result", "failure");
				map.put("jieguo", "修改失败,姓名不能为空...");
				return;
			}
		}else {
			map.put("result", "failure");
			map.put("jieguo", "修改失败，年龄输入不正确...");
			return;
		}
		
	}
	@Override
	public void deleteByIds(String ids, Map<String, Object> map) {
		String[] split = ids.split(",");
		for(int i=0;i<split.length;i++){
			String id = split[i];
			int cId = Integer.parseInt(id);
			try {
				dao.deleteById(cId);
				map.put("result", "success");
				map.put("jieguo", "删除成功...");
			} catch (SQLException e) {

				map.put("result", "failure");
				map.put("jieguo", "删除失败...");
				e.printStackTrace();
				return;
			}
		}
		
	}

}
